package com.yanqu.road.entity.autodata.wealthscroll;

import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.autodata.AutoDataObject;
import org.apache.mina.util.ConcurrentHashSet;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_u_wealth_scroll_user",desc = "新财神密卷用户数据")
public class WealthScrollUserData extends AutoDataObject {
    @FieldTypeAnnotation(desc = "活动id",select = true,update = true)
    private int activityId;
    @FieldTypeAnnotation(desc = "玩家id",select = true,update = true)
    private long userId;
    @FieldTypeAnnotation(desc = "期数")
    private int no;
    @FieldTypeAnnotation(desc = "本期密卷开始时间")
    private long startTime;
    @FieldTypeAnnotation(desc = "经验")
    private long exp;
    //key 1001\1002 任务编号列表
    @FieldTypeAnnotation(desc = "已领取进度奖励列表")
    private Set<Integer> hasGetReward;
    //101 102 mall表里面的额外参数
    @FieldTypeAnnotation(desc = "当前已购买的类型")
    private Set<Integer> buyType;
    @FieldTypeAnnotation(desc = "复购次数")
    private Map<Integer,Integer> repeatBuyMap;
    @FieldTypeAnnotation(desc = "上次刷新condition时间")
    private long lastRefreshConditionTime;
    @FieldTypeAnnotation(desc = "更新时间")
    private long updateTime;

    public int getActivityId() {
        return activityId;
    }
    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public long getUserId() {
        return userId;
    }
    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }
    }

    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        if (this.no != no) {
            this.no = no;
            setOp(DataStatus.Update);
        }
    }

    public long getStartTime() {
        return startTime;
    }
    public void setStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            setOp(DataStatus.Update);
        }
    }

    public long getExp() {
        return exp;
    }
    public void setExp(long exp) {
        if (this.exp != exp) {
            this.exp = exp;
            setOp(DataStatus.Update);
        }
    }

    public Set<Integer> getHasGetReward() {
        return hasGetReward;
    }
    public void setHasGetReward(Set<Integer> hasGetReward) {
        if (null == hasGetReward){
            return;
        }
        if (this.hasGetReward == null || !this.hasGetReward.equals(hasGetReward)) {
            this.hasGetReward = hasGetReward;
            setOp(DataStatus.Update);
        }
    }

    public Set<Integer> getBuyType() {
        return buyType;
    }
    public void setBuyType(Set<Integer> buyType) {
        if (null == buyType){
            return;
        }
        if (this.buyType == null || !this.buyType.equals(buyType)) {
            this.buyType = buyType;
            setOp(DataStatus.Update);
        }
    }

    public Map<Integer, Integer> getRepeatBuyMap() {
        return repeatBuyMap;
    }
    public void setRepeatBuyMap(Map<Integer, Integer> repeatBuyMap) {
        if (null == repeatBuyMap){
            return;
        }
        if (this.repeatBuyMap == null || !this.repeatBuyMap.equals(repeatBuyMap)) {
            this.repeatBuyMap = repeatBuyMap;
            setOp(DataStatus.Update);
        }
    }

    public long getLastRefreshConditionTime() {
        return lastRefreshConditionTime;
    }
    public void setLastRefreshConditionTime(long lastRefreshConditionTime) {
        if (this.lastRefreshConditionTime != lastRefreshConditionTime) {
            this.lastRefreshConditionTime = lastRefreshConditionTime;
            setOp(DataStatus.Update);
        }
    }

    public long getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(long updateTime) {
        if (this.updateTime != updateTime) {
            this.updateTime = updateTime;
            setOp(DataStatus.Update);
        }
    }


    /**
     * 添加本期购买等级
     * @param level
     * @return
     */

    public boolean addBuyLevel(int level) {
        if (buyType == null) {
            buyType = new ConcurrentHashSet<>();
        }
        Set<Integer> tempBuyType = new ConcurrentHashSet<>(this.buyType);
        boolean add = tempBuyType.add(level);
        setBuyType(tempBuyType);
        setUpdateOption();
        return add;
    }

    /**
     * 删除本期购买等级
     * @param level
     * @return
     */

    public boolean removeBuyLevel(int level) {
        if (buyType == null) {
            buyType = new ConcurrentHashSet<>();
        }
        boolean remove = buyType.remove(level);
        setUpdateOption();
        return remove;
    }

    /**
     * 清空购买等级
     * @return
     */

    public boolean clearBuyType() {
        buyType = new ConcurrentHashSet<>();
        setUpdateOption();
        return true;
    }

    /**
     * 是否购买等级
     * @param level
     * @return
     */

    public boolean hasBuyLevel(int level) {
        if (buyType == null) {
            return false;
        }
        return buyType.contains(level);
    }


    /**
     * 添加领取进度奖励记录
     * @param taskId
     * @return
     */

    public boolean addHasGetReward(int taskId) {
        if (this.hasGetReward == null) {
            this.hasGetReward = new ConcurrentHashSet<>();
        }
        Set<Integer> tmpGetReward = new ConcurrentHashSet<>(this.hasGetReward);
        boolean addSuccess = tmpGetReward.add(taskId);
        setHasGetReward(tmpGetReward);
        setUpdateOption();
        return addSuccess;
    }

    /**
     * 是否已经领奖过
     * @param taskId
     * @return
     */

    public boolean hasGetReward(int taskId){
        if (this.hasGetReward == null) {
            return false;
        }
        return this.hasGetReward.contains(taskId);
    }

    public int getRepeatBuyNum(int type){
        if (repeatBuyMap == null) {
            return 0;
        }
        if (!repeatBuyMap.containsKey(type)) {
            return 0;
        }
        return repeatBuyMap.get(type);
    }

    /**
     * 购买时调用 升档
     */
    public boolean addRepeatBuyNum(int type,int num,int configMax){
        if (this.repeatBuyMap == null) {
            this.repeatBuyMap = new ConcurrentHashMap<>();
        }
        if (!repeatBuyMap.containsKey(type)) {
            repeatBuyMap.put(type,num);
        }else {
            //如果超过上限
            if (repeatBuyMap.get(type)+num > configMax) {
                //设置为上限 否则领不了奖励了
                repeatBuyMap.put(type,configMax);
            }else {
                //设置为老值+num
                repeatBuyMap.put(type,repeatBuyMap.get(type)+num);
            }
        }
        setUpdateOption();
        return true;
    }

    /**
     * 时间到了 本期也没有购买 就要降档了
     */
    public boolean reduceRepeatBuyNum(int type,int reduceNum,int min){
        if (this.repeatBuyMap == null) {
            return false;
        }
        if (!repeatBuyMap.containsKey(type)) {
            return false;
        }
        int buyTime = repeatBuyMap.get(type);
        if (buyTime - reduceNum < min) {
            repeatBuyMap.put(type,min);
        }else {
            repeatBuyMap.put(type,buyTime-reduceNum);
        }
        setUpdateOption();
        return true;
    }
}
